package laba3;

import interfaces.task3.StringDiv;
import interfaces.task3.StringUtils;

/**
 *
 * @author yakimovich
 */
public class StringUtilsDemo {

    private static StringUtils stringUtils = new StringUtilsImpl();
    private static StringDiv stringDiv = new StringDivImpl();

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //a demonstration actions
        stringUtilsDemonstration("abc", "cdx");
        stringUtilsDemonstration("Java", "java");

        //a demonstration actions with null
        stringUtilsDemonstration("abc", null);
        stringUtilsDemonstration(null, "abc");
        stringUtilsDemonstration(null, null);

        //a demonstration of division
        stringDivDemonstration("5.3", "4.25");
        stringDivDemonstration("1", null);
        stringDivDemonstration(null, "1");
        stringDivDemonstration("1a", "2b");
        stringDivDemonstration("1", "0");
    }

    private static void stringUtilsDemonstration(String s1, String s2) {
        System.out.print("s1: ");
        displayString(s1);
        System.out.print("s2: ");
        displayString(s2);
        System.out.print("Inverting s1: ");
        displayInvert(s1);
        displayCompareWords(s1, s2);
        displayParseDouble(s1);
        System.out.println("***********************************************");
    }

    private static void stringDivDemonstration(String s1, String s2) {
        System.out.print("s1: ");
        displayString(s1);
        System.out.print("s2: ");
        displayString(s2);
        System.out.print("Double from s1: ");
        displayParseDouble(s1);
        System.out.print("Double from s2: ");
        displayParseDouble(s2);
        displayDiv(s1, s2);
        System.out.println("***********************************************");
    }

    // Methods for display data on the screen:
    private static void displayString(String s) {
        if (s != null) {
            System.out.println("\"" + s + '"');
        } else {
            System.out.println(s);
        }
    }

    private static void displayInvert(String s) {
        // demonstration a inverting
        try {
            String invert = stringUtils.invert(s);
            System.out.println("\"" + invert + '"');
        } catch (Exception e) {
            System.out.println(e);
//            System.err.println(e.getCause());
//            e.printStackTrace();
        }
    }

    private static void displayCompareWords(String s1, String s2) {
        //demonstration a comparison
        try {
            System.out.println("compare s1 ans s2: \""
                    + stringUtils.compareWords(s1, s2) + '"');
        } catch (Exception e) {
            System.out.println(e);
//            System.err.println(e.getCause());
//            e.printStackTrace();
        }
    }

    private static void displayParseDouble(String s) {
        try {
            double d = stringUtils.parseDouble(s);
            System.out.println(d);
        } catch (Throwable e) {
            System.out.println(e);
//            System.err.println(e.getCause());
//            e.printStackTrace();
        }
    }

    private static void displayDiv(String s1, String s2) {
        try {
            System.out.println("s1 / s2: " + stringDiv.div(s1, s2));
        } catch (Exception e) {
            System.out.println("The division is not possible. " + e);
//            System.err.println(e.getCause());
//            e.printStackTrace();
        }
    }
//    
//    public static void main(String[] args) {
//        stringUtils.parseDouble(null);
//    }
}